// import './assets/main.css'
import './style.css'
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import persist from 'pinia-plugin-persistedstate'
// import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css' // 引入 Element UI 的 CSS
import App from './App.vue'
import router from './router'

const app = createApp(App)
    // app.use(ElementPlus)

app.use(createPinia().use(persist))
app.use(router)
import * as ElementPlusIconsVue from '@element-plus/icons-vue' // 导入所有图标


// 全局注册所有图标组件
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}
app.mount('#app')

import { useUserStore } from '@/stores/UserStore'
import { menuIndex } from "@/api";
import { toRaw } from "vue";

const useuserstore = useUserStore()
if (useuserstore.ID === true) {
    // 添加动态路由
    const type = useuserstore.User === '1' ? '1' : '2';
    const routerType = (res) => {
        if (res.code === 500) return;
        useuserstore.dynamicMenu(res.data);
        toRaw(useuserstore.MenuRouer).forEach((item) => {
            // 给指定的地方添加路由
            router.addRoute("main", item);
        });
        const targetPath = useuserstore.routePath; // 可以是动态变量
        router.push(`/${targetPath}`);
    }

    try {
        const res = await menuIndex({ type });
        routerType(res);
    } catch (error) {
        console.error('获取菜单失败:', error);
        // 可以在这里添加错误处理逻辑，如显示错误提示
    }
}